package com.myopen.controller.cms;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.core.ActionKey;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.base.BaseController;
import com.myopen.base.BaseVo;
import com.myopen.model.WebsiteColumn;
import com.myopen.model.WebsiteContact;
import com.myopen.model.WebsiteContent;

public class CmsController extends BaseController{

	public void index() {
		render("/cms/index.html");
	}
	
	@ActionKey("/cms/static")
	public void staticFile() {
		redirect("/cms/");
		
	}
	
	/**
	 * 获取栏目
	 */
	@ActionKey("/cms/column")
	public void content_column() {
		String parentId=getPara("parentId");
		
		List<Record> list=new ArrayList<Record>();
		if(StrKit.isBlank(parentId)) {
			list=WebsiteColumn.dao.getList();
		}else {
			list=WebsiteColumn.dao.getList(parentId);
		}
		
		BaseVo baseVo=new BaseVo();
		baseVo.setCode("0");
		baseVo.setData(list);
		renderJson(baseVo);
		
	}
	
	/**
	 * 获取内容
	 */
	@ActionKey("/cms/content/list")
	public void content_list() {
		Record record=getSearchRecord();//会自动组装通用的参数,分页
		record.set("code", getPara("code"));
		
		Page<Record> page=WebsiteContent.dao.pagequery(record);
		
		BaseVo baseVo=new BaseVo();
		baseVo.setCode("0");
		baseVo.setData(page);
		
		renderJson(baseVo);
	}
	
	/**
	 * 获取内容
	 */
	@ActionKey("/cms/content/detail")
	public void content_content() {
		
		String id=getPara("id");
		
		Record websiteContent=WebsiteContent.dao.getRecordById(id);
		
		BaseVo baseVo=new BaseVo();
		baseVo.setCode("0");
		baseVo.setData(websiteContent);
		
		renderJson(baseVo);
	}
	
	/**
	 * 保存留言内容
	 */
	@ActionKey("/cms/contact")
	public void contact() {
		WebsiteContact vo=getModel(WebsiteContact.class,"",true);
		Record currUser=new Record();
		
		WebsiteContact.dao.save(vo, currUser);
		
		BaseVo baseVo=new BaseVo();
		baseVo.setCode("0");
		
		renderJson(baseVo);
	}
	
}
